package cn.ym.system.controller;

import cn.ym.basic.exception.BusinessException;
import cn.ym.basic.utils.AjaxResult;
import cn.ym.basic.utils.PageList;
import cn.ym.system.domain.Permission;
import cn.ym.system.query.PermissionQuery;
import cn.ym.system.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：权限
 */
@RestController
@RequestMapping("/permission")
@Api(value = "权限接口" ,description = "处理权限的所有请求")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    /**
     * 接口：添加或修改权限信息
     */
    @ApiOperation(value = "添加或修改权限信息")
    @PutMapping
    public AjaxResult save(@RequestBody Permission permission){

            if(permission.getId()==null){
                permissionService.add(permission);
            }else {
                permissionService.update(permission);
            }
            return new AjaxResult();


    }

    /**
     *接口：删除单个权限信息
     */
    @ApiOperation(value = "删除单个权限信息")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){

            permissionService.delete(id);
            return new AjaxResult();

    }

    /**
     * 接口：查询所有权限信息
     */
    @ApiOperation(value = "查询所有权限信息")
    @GetMapping
    public List<Permission> findAll(){
        return permissionService.findAll();
    }

    /**
     * 接口：查询单个权限信息
     */
    @ApiOperation(value = "查询单个权限信息")
    @GetMapping("/{id}")
    public Permission findOne(@PathVariable("id") Long id){
        return permissionService.findOne(id);
    }


    /**
     * 接口：批量删除权限信息
     */
    @ApiOperation(value = "批量删除权限信息")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[]  ids){

            permissionService.patchDel(ids);
            return new AjaxResult();

    }

    /**
     * 接口：分页查询 + 高级查询
     */
    @ApiOperation(value = "分页查询或高级查询")
    @PostMapping
    public PageList<Permission> queryPage(@RequestBody PermissionQuery permissionQuery){
        return permissionService.queryPage(permissionQuery);
   }



}
